﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Routing;
using System.Web;

namespace MvcEngine.Mvc.Constraints
{
    public class IntegerConstraint : IRouteConstraint
    {
        #region Private fields

        private int? min = null;

        private int? max = null;

        #endregion

        public IntegerConstraint()
        {

        }

        public IntegerConstraint(int? min, int? max)
        {
            this.min = min;
            this.max = max;
        }

        #region IRouteConstraint Members

        public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
        {
            int res;
            bool match = true;
            if (int.TryParse(values[parameterName].ToString(), out res))
            {
                if (max.HasValue)
                {
                    if (res <= max)
                        match = true;
                    else
                        match = false;
                }
                if (min.HasValue)
                {
                    if (res >= min)
                        match = true;
                    else
                        match = false;
                }
                return match;
            }
            return false;
        }

        #endregion
    }
}
